<?php
/**
 * Geneone: Content Management Framework
 * Session Handler
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
class Gene_Session {
	/**
	 * This does nothing because the DB is already opened by Gene::.
	 *
	 * @static
	 * @return bool TRUE
	 */
	function open() {
		// Our DB should already be open.
		return true;
	}
	
	/**
	 * This does nothing because we want to leave the DB open.
	 *
	 * @static
	 * @return bool TRUE
	 */
	function close() {
		// Leave open
		return true;
	}
	
	/**
	 * Returns PHP session data for a session.
	 *
	 * @static
	 * @param string $sessionid ID of PHP session
	 * @return string Serialized session data
	 */
	function read($sessionid) {
		$db =& Gene::getDatabase();
		$q = $db->prepare("SELECT * FROM ".Gene::tableName("session")." WHERE session_id=? AND idle > ?");
		$res = $q->execute(array($sessionid, time()));

		if ($row = $res->fetchRow()) {
			$res->free();
			return $row['sessdata'];
		} else {
			return "";
		}
	}
	
	/**
	 * Updates PHP session data
	 *
	 * @static
	 * @param string $sessionid ID of PHP session
	 * @param string $sessdata Serialized session data
	 * @return bool TRUE
	 */
	function write($sessionid, $sessdata) {
		$db =& Gene::getDatabase();
		
		$affected = $db->extended->autoExecute(Gene::tableName("session"), array(
			"sessdata" => $sessdata,
			"idle" => time() + (Gene::getSetting("sess_idle") * 60),
		), MDB2_AUTOQUERY_UPDATE, "session_id='".$sessionid."'");
		
		// We need to create a new session
		if (!$affected) {
			$db->extended->autoExecute(Gene::tableName("session"), array(
				"session_id" => $sessionid,
				"user_id" => (int)$_SESSION['_authsession']['username'],
				"timestamp" => (int)time(),
				"idle" => (int)(time() + (Gene::getSetting("sess_idle") * 60)),
				"sessdata" => $sessdata,
			), MDB2_AUTOQUERY_INSERT);
		}
		
		return true;
	}
	
	/**
	 * Updates member data in session table. We need to call
	 * this when the user logs in or switches account.
	 *
	 * @static
	 * @param string $sessionid ID of PHP session
	 * @param string $sessdata Serialized session data
	 * @return bool TRUE
	 */
	function updateSessData($sessionid) {
		$db =& Gene::getDatabase();
		
		$db->extended->autoExecute(Gene::tableName("session"), array(
			"user_id" => $_SESSION['_authsession']['username'],
		), MDB2_AUTOQUERY_UPDATE, "session_id='".$sessionid."'");
		
		return true;
	}
	
	/**
	 * Destroys a PHP session.
	 *
	 * @static
	 * @param string $sessionid ID of PHP session
	 * @return bool TRUE
	 */
	function destroy($sessionid) {
		$db =& Gene::getDatabase();
		
		$q = $db->prepare("DELETE FROM ".Gene::tableName("session")." WHERE session_id=?");
		$q->execute(array($sessionid));
		
		return true;
	}
	
	/**
	 * Remove old sessions and things.
	 *
	 * @static
	 * @var int $lifetime Session Lifetime in Seconds
	 * @return bool TRUE
	 */
	function gc($lifetime=null) {
		$db =& Gene::getDatabase();
		
		$q = $db->prepare("DELETE FROM ".Gene::tableName("session")." WHERE idle < ? OR timestamp=0");
		$q->execute(array(time()));
		
		/* Destroy auto logins older than 180 days */
		$q = $db->prepare("DELETE FROM ".Gene::tableName("auto_login")." WHERE timestamp < ?");
		$q->execute(array(time()-15552000));
		
		return true;
	}
	
	/**
	 * Save session so we can close DB and stuff.
	 *
	 * @static
	 * @return bool TRUE
	 */
	function shutdown() {
		session_write_close();
	}
}
 
?>